﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Doo.DataSource;

namespace Doo.Controls
{
    /* 
     * TODO：定时刷新功能
     */

    #region 客户端事件定义

    public class ObjDataSourceClientEvents : Doo.Scripts.ClientEventsBase
    {
        /// <summary>
        /// 选择之前触发
        /// </summary>
        [Doo.WebUI.ClientEvent("selecting")]
        public Doo.Scripts.ClientEvent Selecting { get; set; }

        /// <summary>
        /// 选择完成后触发
        /// </summary>
        [Doo.WebUI.ClientEvent("selected")]
        public Doo.Scripts.ClientEvent Selected { get; set; }

        public ObjDataSourceClientEvents()
        {
            this.Selecting = new Scripts.ClientEvent();
            this.Selected = new Scripts.ClientEvent();
        }
    }

    #endregion

    [ToolboxData("<{0}:ObjDataSource runat=server></{0}:ObjDataSource>")]
    public class ObjDataSource : DataSourceBase,Doo.Scripts.IClientEvent<ObjDataSourceClientEvents>
    {
        const string _ScriptName = "Doo.WebUI.DataSource.ObjDataSource.script.js";

        #region 基本属性设置

        /// <summary>
        /// 类型
        /// </summary>
        public override string ClientControlType
        {
            get
            {
                return "Doo.DataSource.ObjDataSource";
            }
        }

        /// <summary>
        /// 资源名
        /// </summary>
        public override string ScriptResourceName
        {
            get
            {
                return _ScriptName;
            }
        }

        #endregion

        #region 客户端事件

        public ObjDataSourceClientEvents ClientEvents { get; set; }

        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.ClientEvents != null)
            {
                this.ClientEvents.Init(this.Page, this.Parent);
            }
        }

        [Description("获取数据的方法")]
        public string SelectMethod { get; set; }

        [Description("对象名")]
        public string ObjectName { get; set; }

        [Description("增加方法")]
        public string InsertMethod { get; set; }

        [Description("更新方法")]
        public string UpdateMethod { get; set; }

        [Description("删除方法")]
        public string DeleteMethod { get; set; }

        #region 缓存[版本号]

        public bool EnableVersion { get; set; }

        public string VersionKey { get; set; }

        public string VersionKeyFormatString { get; set; }

        #endregion

        protected override void SetScriptDescriptor(ScriptControlDescriptor descriptor)
        {
            base.SetScriptDescriptor(descriptor);
            descriptor.AddProperty("selectmethod", SelectMethod);
            if (SelectParameters != null) descriptor.AddProperty("initselectargs", Parameter.ToArrayForClient(SelectParameters, Parent));
            descriptor.AddProperty("objname", ObjectName);
            if (EnableVersion)
            {
                //var cm = Page.GetVersionManager();
                //if (cm == null) throw new Exception("页面上必须有一个Doo.VersionManager且应该在Doo.DataSource之前");
                descriptor.AddProperty("enableversion", true);
                string k = Caching.PageCacheHelper.GetKey(this.VersionKey);
                descriptor.AddProperty("versionkey", k);
            }
        }

    }
}
